//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ System.Serializable ]
public class BaseNode : MonoBehaviour {

	/// <summary>
	/// Play this Node.
	/// </summary>
	virtual public void Play(){}

	/// <summary>
	/// Scenario script.
	/// </summary>
	/// <param name="sb">Sb.</param>
	virtual public void ToScenarioScript( ref System.Text.StringBuilder sb ){}

	/// <summary>
	/// To byte code script.
	/// </summary>
	virtual public void ToByteCode( ByteCodes code ){}

	/// <summary>
	/// Serializes as XML string.
	/// </summary>
	/// <returns>The XM.</returns>
	/// <param name="fileName">File name.</param>
	virtual public string SerializeXML( string fileName ){
		return string.Empty;
	}
	
	/// <summary>
	/// Deserializes from a XML string.
	/// </summary>
	/// <param name="xmlStr">Xml string.</param>
	virtual public void DeserializeXML( string xmlStr ){}

	/// <summary>
	/// Gets this node label.
	/// </summary>
	/// <returns>The node label.</returns>
	virtual public string GetNodeLabel(){
		return GetNodeTag( name );
	}

	public void AddNodeCode( List<byte> byteList ){		
		string label = GetNodeLabel();
		ByteCodeScriptTools.AddNodeCode( byteList , label );		
	}

	//  Parent Name + "_" + NodeName.
	public void AddNodeCodeWithTag( List<byte> byteList , string tag ){		
		ByteCodeScriptTools.AddNodeCode( byteList , tag );		
	}

	public string GetNodeTag( string tag ){			
		System.Text.StringBuilder sb = new System.Text.StringBuilder();
		if( transform.parent != null ){
			if( transform.parent.parent != null) {
				sb.Append( transform.parent.parent.name + "/" );	
				sb.Append( transform.parent.name + "/" );					
			}
			else{
				sb.Append( transform.parent.name + "/" );	
			}
		}								
		sb.Append( tag );			
		return sb.ToString();	
	}

	
	/// <summary>
	/// Compile this instance.
	/// </summary>
	public ByteCodes Compile(){
		ByteCodes btcodes = new ByteCodes();
		btcodes.Init();
		
		ToByteCode( btcodes );		
		
		return btcodes;
	}
	
	public ByteCodes CompileInternalCode(){
		ByteCodes btcodes = new ByteCodes();
		btcodes.Init();
		
		ToByteCodeInternal( btcodes );		
		
		return btcodes;
	}
	
			
	// --------------- protected Methods --------------------------------------.			

	protected void ToByteCodeInternal( ByteCodes code ){
		ViNoDebugger.Log ( "Compile" , name + " is compile" );
		int childNum = transform.GetChildCount();
		if( childNum > 0 ){
			for( int i=0;i<childNum;i++){
				Transform child = transform.GetChild( i );				
				if( child.gameObject.activeInHierarchy ){
					ViNoDebugger.Log ( "Compile" , "child "+ child.name + " is added." );
					BaseNode[] nodes = child.GetComponents<BaseNode>();		// Be careful , This is getcomponent"S".
					if( nodes != null ){
						for( int k=0;k<nodes.Length;k++){
							if( nodes[ k ].enabled ){
								nodes[ k ].ToByteCode( code );

							}
						}
					}					
				}
			}
			code.Add( Opcode.STOP );
		}	
		
#if false			
		// If Deactivate at Start  , Add to GOCache.
		if( ! startAndActive ){
			GOCache.Add( this.gameObject );	
		}
		gameObject.SetActive( startAndActive );		
#endif			
	}
		
	protected void NotEditable( ){
		if( Application.isPlaying) {
			gameObject.hideFlags = HideFlags.NotEditable;		
		}				
	}

}
